<?php

namespace app\admin\controller\deerhome;

use app\common\controller\Backend;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Djq extends Backend
{

    /**
     * Djq模型对象
     * @var \app\admin\model\deerhome\Djq
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\deerhome\Djq;
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

/**
* 添加
*
* @return string
* @throws \think\Exception
*/
    public function add()
    {
        $ids = $this->request->param('ids');
        $order_info = db('deerhome_order')->where('id',$ids)->find();
        $exist = $this->model->where('order_sn',$order_info['sn'])->find();
        $row_info = $exist;
        if(empty($exist['money_yh'])){
            $row_info['status'] = 1;
            $row_info['money_yh'] = '';
        }
        $this->view->assign('order_info',$order_info);
        $this->view->assign('row',$row_info);

        if (false === $this->request->isPost()) {
            return $this->view->fetch();
        }


        $params = $this->request->post('row/a');
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }
        if(!empty($exist)){
            $row = $exist;
            $params['id'] = $exist['id'];
        }else{
            $params['get_time'] = time();
            $params['start_day'] = time();
            $code=$this->generateCode(1);
            $params['order_sn'] = $order_info['sn'];
            $params['code'] = $code[0];
            $params['end_day'] = strtotime('2030-01-01 00:00:00');
            $row = $this->model;
        }
        if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
            $params[$this->dataLimitField] = $this->auth->id;
        }
        $params = $this->preExcludeFields($params);
        $result = false;
        Db::startTrans();
        try {
            $result = $row->allowField(true)->save($params);
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result === false) {
            $this->error(__('No rows were inserted'));
        }
        $this->success();
    }


    /**
     * 生成优惠码
     * @param int $nums             生成多少个优惠码
     * @param array $exist_array     排除指定数组中的优惠码
     * @param int $code_length         生成优惠码的长度
     * @param int $prefix              生成指定前缀
     * @return array                 返回优惠码数组
     */
    private function generateCode( $nums,$exist_array='',$code_length = 12,$prefix = '' ) {

        $characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnpqrstuvwxyz";
        $promotion_codes = array();//这个数组用来接收生成的优惠码
        for($j = 0 ; $j < $nums; $j++) {
            $code = '';
            for ($i = 0; $i < $code_length; $i++) {

                $code .= $characters[mt_rand(0, strlen($characters)-1)];
            }
            //如果生成的4位随机数不再我们定义的$promotion_codes数组里面
            if( !in_array($code,$promotion_codes) ) {
                if( is_array($exist_array) ) {
                    if( !in_array($code,$exist_array) ) {//排除已经使用的优惠码
                        $promotion_codes[$j] = $prefix.$code; //将生成的新优惠码赋值给promotion_codes数组
                    } else {
                        $j--;
                    }
                } else {
                    $promotion_codes[$j] = $prefix.$code;//将优惠码赋值给数组
                }
            } else {
                $j--;
            }
        }
        return $promotion_codes;
    }

}
